<?php

namespace App\Http\Validate\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Http\Validate\BaseValidate;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Kuafu\File\FileUtil;

/**
 * [ 订单业务验证 ] - 校验规则
 *
 * @OpenShopUserContractValidate
 * @package App\Http\Controllers
 * @date 2022/03/01 02:35:56
 * @author GreenLeaf <786407418@qq.com>
 */
class OrderValidate extends BaseValidate
{

    public static function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    /**
     * @var array|string[]
     */
    public static array $validateMsg = [
        "orderNo.exists" => "订单已失效或当前状态不支持此操作"
    ];

    /**
     * 获取规则信息
     *
     * @param array $fields
     * @return string[]
     */
    public static function getRules(array $fields = []): array
    {
        return self::getActuallyRules([
            'orderNo' => 'bail|required|exists:' . OpenShopSignOrderModel::class . ',orderNo',//
        ],
            $fields);
    }

    /**
     * 详情校验
     *
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function detailValidate(array $requestParams): array
    {
        return self::baseValidate($requestParams, ['orderNo'], null, [], self::getValidateMsg());
    }

    /**
     * 作废订单校验
     *
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function invalidOrderValidate(array $requestParams): array
    {
        return self::baseValidate(
            $requestParams,
            ["orderNo", "reason"],
            null,
            [
                'orderNo'     => [
                    'required',
                    Rule::exists('open_shop_sign_order')->where(function ($query) {
                        $query->where('status', "<>", SignOrderEnum::ORDER_STATUS_NO);
                    })
                ],
                "reason"      => "required|string|min:1|max:120"
            ],
            self::getValidateMsg()
        );
    }

    /**
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function submitOrderProofValidate(array $requestParams): array
    {
        return self::baseValidate(
            $requestParams,
            ['orderNo', "proof"],
            function ($params) {
                foreach ($params["proof"] as &$eachProof) {
                    $eachProof["img"] = getPrivateFileKey($eachProof["img"]);
                }
                return $params;
            },
            [
                'orderNo' => [
                    'required',
                    Rule::exists('open_shop_sign_order')->where(function ($query) {
                        $query->where('status', UserOrderEnum::STATUS_NORMAL)
                              ->where('step', '<=', UserOrderEnum::STEP_PART_PAID);
                    }),
                ],
                'proof'   => [
                    'bail',
                    'required',
                    'array',
                    function ($attribute, $eachProof, $fail) {
                        foreach ($eachProof as $value) {
                            if (!isset($value["price"]) || !is_int($value["price"]) || !$value["price"] > 0) {
                                $fail("凭证金额缺失或金额格式非法");
                            }

                            if (!isset($value["img"])) {
                                $fail("凭证图片缺失");
                            }
                        }
                    },
                ]
            ],
            self::getValidateMsg()
        );
    }

    /**
     * @param array $requestParams
     * @return object
     * @throws ValidationException
     */
    public static function changeManagerValidate(array $requestParams): object
    {
        return (object)self::baseValidate(
            $requestParams,
            ["id", "type", "managerId", "managerName"],
            null,
            [
                'id'          => [
                    'int',
                    'required',
                    Rule::exists("open_shop_order_manager", "id")->where(function ($query) {
                        $query->where('orderStatus', OpenShopOrderManagerModel::ORDER_STATUS_NORMAL)
                              ->where('isDeleted', OpenShopOrderManagerModel::NORMAL);
                    }),
                ],
                'type'        => 'required|int|in:' . enumTransToRules(OpenShopOrderManagerModel::TYPE_MAP),
                'managerId'   => 'required|int|max:1000000',
                'managerName' => 'required|string|max:120'
            ],
            self::getValidateMsg()
        );
    }

    /**
     * @param array $requestParams
     * @return object
     * @throws ValidationException
     */
    public static function getOrderCityFeeValidate(array $requestParams): object
    {
        return (object)self::baseValidate(
            $requestParams,
            ["orderNo"],
            null,
            [
                'orderNo' => [
                    'required',
                    Rule::exists("open_shop_location", "orderNo")->where(function ($query) {
                        $query->where('cityCode', ">", 0);
                    }),
                ]
            ],
            self::getValidateMsg()
        );
    }

    /**
     * @param array $requestParams
     * @return object
     * @throws ValidationException
     */
    public static function changeAppletManagerValidate(array $requestParams): object
    {
        return (object)self::baseValidate(
            $requestParams,
            ["id", "type", "managerId"],
            null,
            [
                'id'        => [
                    'int',
                    'required',
                    Rule::exists("open_shop_order_manager", "id")->where(function ($query) {
                        $query->where('orderStatus', OpenShopOrderManagerModel::ORDER_STATUS_NORMAL)
                              ->where('isDeleted', OpenShopOrderManagerModel::NORMAL);
                    }),
                ],
                'type'      => 'required|int|in:' . enumTransToRules(OpenShopOrderManagerModel::TYPE_MAP),
                'managerId' => 'required|int|max:1000000',
            ],
            self::getValidateMsg()
        );
    }

    /**
     * 筹备订单详情校验
     *
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function preparationDetailValidate(array $requestParams): array
    {
        return self::baseValidate(
            $requestParams, ['orderNo'], null, [
            'orderNo' => [
                'required',
                Rule::exists('open_shop_sign_order')->where(
                    function ($query) {
                        $query->whereIn('step', MissionTemplateEnum::PREPARE_STATUS);//筹备中状态
                    }
                ),
            ]
        ], self::getValidateMsg()
        );
    }
}
